import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Login from '../views/Login.vue'
import About from '../views/AboutView.vue'

import store from '../stores'; // 假设你的 store 在上级目录


const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta: { requiresAuth: true, showHeader: true, showFooter: true }
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
    meta: { requiresAuth: false, showHeader: false, showFooter: false }
  },
  {
    path: '/about',
    name: 'about',
    component: About,
    meta: { requiresAuth: false, showHeader: false, showFooter: false }
  }

];

const router = createRouter({
  history: createWebHistory(),
  routes
});


router.beforeEach((to, from, next) => {
  const isAuthenticated = store.state.isLoggedIn;

    if (!isAuthenticated && to.name !== 'login') {
      next({ name: 'login' });
    } else {
      next();
    }
});
export default router
